﻿using MvvmLib;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using LaserLineDetectorLib.ViewModel;
using Glue_FrameWork.ViewModels.Param;
using CatLogLib;
using CheckPermit;

namespace LaserLineDetector.ViewModel
{
    public class Vm_MainWindow : Vm_Base
    {
        /// <summary>
        /// 调度
        /// </summary>
        public Vm_LaserLineDetector_Dispatch Dispatch
        {
            get { return _Dispatch; }
            set
            {
                if (_Dispatch != value)
                {
                    _Dispatch = value;
                    this.OnPropertyChanged("Dispatch");
                }
            }
        }
        private Vm_LaserLineDetector_Dispatch _Dispatch;

        /// <summary>
        /// 调度参数
        /// </summary>
        public Vm_LaserLineDetector_Dispatch_Param Param
        {
            get { return _Param; }
            set
            {
                if (_Param != value)
                {
                    _Param = value;
                    this.OnPropertyChanged("Param");
                }
            }
        }
        private Vm_LaserLineDetector_Dispatch_Param _Param;

        /// <summary>
        /// 版本号
        /// </summary>
        public string Version
        {
            get { return _Version; }
            set
            {
                if (_Version != value)
                {
                    _Version = value;
                    this.OnPropertyChanged("Version");
                }
            }
        }
        private string _Version;

        public Vm_MainWindow()
        {
#if DEBUG

            //验证是否有使用权限
            try
            {
                bool isValid = Vm_CheckPermit.CheckPermit("license.dat", "http://119.91.20.14/check/");
                if (!isValid)
                {
                    CatLog.Log("主程序", E_CatLogType.错误, "License不可用，程序无法正常使用！");
                }
            }
            catch (Exception ex)
            {
                CatLog.Log("主程序", E_CatLogType.错误, "License在线验证失败：" + ex.Message);
            }

#endif

            //获取当前运行目录
            string currentPath = Environment.CurrentDirectory;

            //参数文件路径
            string paramFilePath = currentPath + @"\Param.xml";

            try
            {
                //读取参数
                Vm_LaserLineDetector_Dispatch_Param dispatchParam = new Vm_LaserLineDetector_Dispatch_Param();
                GParam.Load(ref dispatchParam, paramFilePath);
                Param = dispatchParam;

            }
            catch (Exception)
            {
                this.Param = new Vm_LaserLineDetector_Dispatch_Param()
                {
                    Path_File = paramFilePath
                };

                CatLog.Log("主程序", E_CatLogType.警告, "参数文件不存在，将使用默认参数");
            }

            this.Dispatch = new Vm_LaserLineDetector_Dispatch(this.Param);
        }

    }
}
